# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  
  # methods for deal thickbox js framework
  def link_to_out text , path , html ={} ,tb={}
    html_opts ={
      :title => ""  ,
      :class => "thickbox"    
    }.merge!(html)
    tb_opts = {
      :height  => 300,
      :width  => 400
    }.merge!(tb)
    url = path.add_params(tb_opts)
    link_to(text,url,html_opts)
  end
  
  def link_to_in text, inlineId,html ={} ,tb={}
    html_opts = {
      :title => '',
      :class => 'thickbox'
    }.merge!(html)
    tb_opts = {
      :height => 300,
      :width => 400,
      :inlineId => inlineId
    }.merge!(tb)
    
    path = '#TB_inline'.add_params(tb_opts)
    link_to(text, path, html_opts)
  end

  
  def display_standard_flashes(message = 'There were some problems with your submission:')
    if flash[:notice]
      flash_to_display, level = flash[:notice], 'notice'
    elsif flash[:warning]
      flash_to_display, level = flash[:warning], 'warning'
    elsif flash[:error]
      level = 'error'
      if flash[:error].instance_of?( ActiveRecord::Errors) || flash[:error].is_a?( Hash)
        flash_to_display = message
        flash_to_display << activerecord_error_list(flash[:error])
      else
        flash_to_display = flash[:error]
      end
    else
      return
    end
    content_tag 'div', flash_to_display, :class => "flash#{level}"
  end

  def activerecord_error_list(errors)
    error_list = '<ul class="error_list">'
    error_list << errors.collect do |e, m|
      "<li>#{e.humanize unless e == "base"} #{m}</li>"
    end.to_s << '</ul>'
    error_list
  end
  
end
